package com.RuiJi.service.Impl;

import com.RuiJi.entity.Setmeal;
import com.RuiJi.entity.SetmealDish;
import com.RuiJi.entity.dto.SetmealDto;
import com.RuiJi.mapper.SetmealMapper;
import com.RuiJi.service.SetmealDishService;
import com.RuiJi.service.SetmealService;
import com.RuiJi.utils.CustomException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Twinkle
 * @PACKAGE_NAME: com.RuiJi.service.Impl
 * @NAME: SetmealServiceImpl
 * @USER: Twinkle
 * @DATE: 2023/4/7
 * @TIME: 17:31
 * @YEAR: 2023
 * @MONTH: 04
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public boolean saveWithDish(SetmealDto setmealDto) {
        if (!this.save(setmealDto)) {
            return false;
        }
        //获取套餐关系
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(String.valueOf(setmealDto.getId()));
            return item;
        }).collect(Collectors.toList());
        if (!setmealDishService.saveBatch(setmealDishes)) {
            return false;
        }

        return true;
    }

    @Override
    public void removeWithDish(List<Long> ids) {

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("套餐正在售卖，不能删除");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(wrapper);
    }

    @Override
    public SetmealDto getByIdWithSetMealDish(Long id) {

        //查询套餐基本信息
        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();

        //将查询到的套餐基本信息拷贝到setmealDto中
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询SetmealDish中与套餐id相同的数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        //将setmeal_dish中的
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(String.valueOf(setmealDto.getId()));
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }


}
